﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Grid.aspx.cs" Inherits="RightsRegistrationWeb.Grid" %>

<html>
<head>
    <link rel="Stylesheet" href="CSS/Right.css" />

    <script language="javascript">
        function GridOnMouseOver(linha) {
            if (linha.bgColor != "#c0c0c0")
                linha.bgColor = "#f0f0f0";
        }

        function GridOnMouseOut(linha) {
            if (linha.bgColor != "#c0c0c0")
                linha.bgColor = "#ffffff";
        }

        function GridMouseDblClick(dataGrid, linha, modules) {
            grid = eval(dataGrid);
            var rows = grid.getElementsByTagName("tr");

            for (i = 0; i < rows.length; i++) {
                rows[i].bgColor = "#ffffff";
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox")
                        inputs[j].checked = false;
                }
            }

            linha.bgColor = "#c0c0c0";
            linha.getElementsByTagName("input")[0].checked = true;

            tds = linha.getElementsByTagName("td");
            codigoDireito = tds[1].firstChild.data;
            descricaoDireito = tds[2].firstChild.data;
            codigoTipoDireito = linha.codigotipodireito;
            codigoTipoOperacao = linha.codigotipooperacao;

            SetParentForm(codigoDireito, descricaoDireito, codigoTipoDireito, codigoTipoOperacao, modules);

            parent.rightsForm.btDeleteSelected.disabled = false;
            parent.rightsForm.cbTipoDireito.disabled = true;

        }

        function GridMouseClick(datagrid, idCheckBox) {
            parent.rightsForm.hdnCodigoDireito.value = "";
            parent.rightsForm.txtDescricao.value = "";
            parent.rightsForm.cbTipoDireito.selectedIndex = 0;
            parent.rightsForm.cbTipoOperacao.selectedIndex = 0;

            grid = eval(datagrid);
            var rows = grid.getElementsByTagName("tr");

            for (i = 1; i < rows.length; i++) {
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox" && inputs[j].id == idCheckBox) {
                        if (inputs[j].checked) {
                            rows[i].bgColor = "#ffffff";
                            inputs[j].checked = false;
                        }
                        else {
                            rows[i].bgColor = "#c0c0c0";
                            inputs[j].checked = true;
                        }
                        break;
                    }
                }
            }

            GetSelectedItems(grid);
        }

        function GetSelectedItems(grid) {
            parent.rightsForm.hdnSelected.value = "";

            var rows = grid.getElementsByTagName("tr");

            for (i = 1; i < rows.length; i++) {
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox" && inputs[j].checked) {
                        parent.rightsForm.hdnSelected.value += inputs[j].id + ",";
                    }
                }
            }

            parent.rightsForm.hdnSelected.value = parent.rightsForm.hdnSelected.value.substring(0, parent.rightsForm.hdnSelected.value.length - 1);
            if (parent.rightsForm.hdnSelected.value.length == 0)
                parent.rightsForm.btDeleteSelected.disabled = true;
            else
                parent.rightsForm.btDeleteSelected.disabled = false;
        }

        function SetParentForm(codigoDireito, descricaoDireito, codigoTipoDireito, codigoTipoOperacao, modules) {
            parent.rightsForm.hdnSelected.value = "";
            parent.rightsForm.hdnCodigoDireito.value = codigoDireito;
            parent.rightsForm.txtDescricao.value = descricaoDireito.replace("http://www.bovespa.com.br/", "");

            for (i = 0; i < parent.rightsForm.cbTipoDireito.length; i++) {
                if (parent.rightsForm.cbTipoDireito[i].value == codigoTipoDireito) {
                    parent.rightsForm.cbTipoDireito.selectedIndex = i;
                    break;
                }
            }

            for (i = 0; i < parent.rightsForm.cbTipoOperacao.length; i++) {
                if (parent.rightsForm.cbTipoOperacao[i].value == codigoTipoOperacao) {
                    parent.rightsForm.cbTipoOperacao.selectedIndex = i;
                    break;
                }
            }

            var table = parent.document.getElementById('tblModules');

            for (i = 0; i < table.rows.length; i++) {
                for (j = 0; j < table.rows[i].cells.length; j++) {
                    if (table.rows[i].cells[j].children.length > 0) {
                        var chk = table.rows[i].cells[j].children[0];
                        chk.checked = false;
                    }
                }
            }

            var module = modules.split(',');

            for (i = 0; i < module.length; i++) {
                var chk = parent.document.getElementById(module[i]);
                if (chk != null)
                    chk.checked = true;
            }

        }

        function ResetGridColor(datagrid) {
            grid = eval(datagrid);
            var rows = grid.getElementsByTagName("tr");

            for (i = 1; i < rows.length; i++) {
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox") {
                        inputs[j].checked = false;
                        rows[i].bgColor = "#ffffff";
                    }
                }
            }
        }
    </script>

</head>
<body style="margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;">
    <form id="grdForm" runat="server">
    <asp:GridView runat="server" AllowPaging="True" AllowSorting="True" CellPadding="2"
        CellSpacing="1" ForeColor="#333333" GridLines="None" PageSize="12" Width="100%"
        ID="grdRights" AutoGenerateColumns="False" OnPageIndexChanging="grdRights_PageIndexChanging"
        OnRowDataBound="grdRights_RowDataBound">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField HeaderText="&nbsp;" HeaderStyle-Width="25px" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="chkRight" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Codigo" DataField="CodigoDireito" HeaderStyle-Width="55px" ItemStyle-HorizontalAlign="Center" />
            <asp:BoundField HeaderText="Descrição" DataField="DescricaoDireito"  HeaderStyle-HorizontalAlign="Left" />
            <asp:BoundField HeaderText="Tipo Direito" DataField="DescricaoTipoDireito" HeaderStyle-HorizontalAlign="Left" />
            <asp:TemplateField HeaderText="Tipo Operação" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="90px" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblGrdTipoOperacao" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>
